package com.sugar.subscribe.course.module.business.controller.admin.coachcomment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.sugar.subscribe.course.framework.common.pojo.PageParam;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.util.object.BeanUtils;
import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;

import com.sugar.subscribe.course.framework.excel.core.util.ExcelUtils;

import com.sugar.subscribe.course.framework.apilog.core.annotation.ApiAccessLog;
import static com.sugar.subscribe.course.framework.apilog.core.enums.OperateTypeEnum.*;

import com.sugar.subscribe.course.module.business.controller.admin.coachcomment.vo.*;
import com.sugar.subscribe.course.module.business.dal.dataobject.coachcomment.CoachCommentDO;
import com.sugar.subscribe.course.module.business.service.coachcomment.CoachCommentService;

@Tag(name = "管理后台 - 评论")
@RestController
@RequestMapping("/business/coach-comment")
@Validated
public class CoachCommentController {

    @Resource
    private CoachCommentService coachCommentService;

    @PostMapping("/create")
    @Operation(summary = "创建评论")
    //@PreAuthorize("@ss.hasPermission('business:coach-comment:create')")
    public CommonResult<Long> createCoachComment(@Valid @RequestBody CoachCommentSaveReqVO createReqVO) {
        return success(coachCommentService.createCoachComment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评论")
    //@PreAuthorize("@ss.hasPermission('business:coach-comment:update')")
    public CommonResult<Boolean> updateCoachComment(@Valid @RequestBody CoachCommentSaveReqVO updateReqVO) {
        coachCommentService.updateCoachComment(updateReqVO);
        return success(true);
    }

    @PutMapping("/audit")
    @Operation(summary = "审核评论")
    public CommonResult<Boolean> auditCoachComment(@Valid @RequestBody CoachCommentAuditReqVO auditReqVO) {
        coachCommentService.auditCoachComment(auditReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评论")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('business:coach-comment:delete')")
    public CommonResult<Boolean> deleteCoachComment(@RequestParam("id") Long id) {
        coachCommentService.deleteCoachComment(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除评论")
    //@PreAuthorize("@ss.hasPermission('business:coach-comment:delete')")
    public CommonResult<Boolean> deleteCoachCommentList(@RequestParam("ids") List<Long> ids) {
        coachCommentService.deleteCoachCommentListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('business:coach-comment:query')")
    public CommonResult<CoachCommentRespVO> getCoachComment(@RequestParam("id") Long id) {
        CoachCommentDO coachComment = coachCommentService.getCoachComment(id);
        return success(BeanUtils.toBean(coachComment, CoachCommentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得评论分页")
    //@PreAuthorize("@ss.hasPermission('business:coach-comment:query')")
    public CommonResult<PageResult<CoachCommentRespVO>> getCoachCommentPage(@Valid CoachCommentPageReqVO pageReqVO) {
        return success(coachCommentService.getCoachCommentPage(pageReqVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评论 Excel")
    //@PreAuthorize("@ss.hasPermission('business:coach-comment:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCoachCommentExcel(@Valid CoachCommentPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        // 导出 Excel
        ExcelUtils.write(response, "评论.xls", "数据", CoachCommentRespVO.class,
                coachCommentService.getCoachCommentPage(pageReqVO).getList());
    }

}